<?php
/**
 * maker: 梁康宁
 * Time: 2019-09-02 21:58:27
*/
namespace app\dlc\controller;
use app\common\model\FormList;
use app\dlc\common\createHelper\CreateController;
use app\dlc\common\createHelper\CreateModel;
use app\dlc\common\form\Form;
use think\Db;

class FormListController extends CommonController
{

	public function index()
	{
		$this->headBotton('','add','添加',CONTROLLER_URL.'_edit');
        $this->listBotton('','edit','编辑',CONTROLLER_URL . 'edit',2);
        $this->listBotton('','enable','初始化',CONTROLLER_URL . '_edit_ajax',2,'','','',['status','=','0']);
        $this->listBotton('','device','列表配置','/dlc/FormListItem/index','','','');
        $this->listBotton('','add','生成Controller',CONTROLLER_URL . 'addController',1,'确定生成Controller？生成会覆盖原文件','btn-purple');
        $this->listBotton('','add','生成Model',CONTROLLER_URL . 'addModel',1,'确定生成Model？生成会覆盖原文件');
        $this->listBotton('','delete','删除',CONTROLLER_URL . '_del');
		$this->loading();
		$map = $this->map;
		$data = model(CONTROLLER_NAME)->where($map)->order('id desc')->page(self::$page)->select();
		$count = model(CONTROLLER_NAME)->where($map)->count();
		$this->getPage($count, self::$psize, 'App-loader', $this->title, 'App-search');
		$this->assign('data',$data);
		$list = ['id'=>'ID','table'=>'表名','ctime'=>'创建时间','sys'=>'操作'];
		$this->assign('list',$list);
		echo $this->fetch('common/index');
	}




	//$funName 添加和编辑成功后的回调方法,返回 $id,$action , $action =add 是添加，$action = edit 是编辑
    public function _edit($updata = false, $add = false, $noRepeat = false, $funName = false)
    {

//        $updata['table'] = "";//更新的表，默认自动获取
//        $updata['key'] = "";//表的主键，默认自动获取
//        $noRepeat['map'] = "";//新增判断条件
        $noRepeat['name'] = "table";//字段，不可重复的字段
        $noRepeat['tig'] = "表名不能重复";//提示语
        parent::_edit($updata, $add, $noRepeat, 'add_role_oath');
    }


    public function edit(){
	    $this->form = 'form_list';
	    parent::_edit_ajax('','','','add_role_oath');

    }


    //添加菜单
    public function add_role_oath($id,$action){
       $fonmList = model(CONTROLLER_NAME)->find($id);
       $data['name'] = $fonmList['name'];
       $data['url'] = $this->camelize($fonmList['table'],true).'/index';
       $data['parent_id'] = $fonmList['parent_id'];
       if ($data['parent_id']){
           if ($action=='edit'){
               $res = Db::name('role_oath')->where(['oath_id'=>$fonmList['oath_id']])->update($data);
               if ($res) admin_return(1,'成功');
           }else{
               $oath_id = Db::name('role_oath')->insertGetId($data);
               $res = Db::name(CONTROLLER_NAME)->where(['id'=>$id])->update(['oath_id'=>$oath_id]);
               $Dlc_auth = model('role') -> where(array('role_id' => 1)) -> value('auth');
               Db::name('role') ->where(['role_id' => 1])->update(['auth' => $Dlc_auth.",".$oath_id]);
               if ($res) admin_return(1,'成功');
           }
       }else{
           admin_return(1,'成功');
       }

       admin_return(0,'失败');
    }


    //配置
    public function _edit_ajax($updata = false, $add = false, $noRepeat = false, $funName = false)
    {
        $post  = input('post.');
        $id = input('id');
        $table = Db::name(CONTROLLER_NAME)->where(['id'=>$id])->value('table');
        if (count($post)>1){
            $fields = self::get_db_column_comment($table);
            $head_botton = $post['head_botton'];
            $head_search = $post['head_search'];
            $list_botton = $post['list_botton'];
            $list_title = $post['list_title'];
            $sort = 1;
            if (!in_array($table,self::$roleList)){
                $urlList = ['add'=>'_edit','back'=>'index','send'=>'check',];
            }else{
                $urlList = ['add'=>'roleEdit','back'=>'index','send'=>'check',];
            }
            foreach ($head_botton as $key=>$value){
                $data['form_list_id'] = $id;
                $data['table'] = $table;
                $data['type'] = 1;
                $data['key'] = $value;
                $data['url'] = $urlList[$value]?:'';
                $data['sort'] = $sort;
                $data['name'] = Form::$head_botton_array[$value]?:'';
                $data['ctime'] = time();
                model('form_list_item')->insert($data);
                $sort++;
            }

            $sort = 1;
            foreach ($head_search as $key=>$value){
                $data['form_list_id'] = $id;
                $data['table'] = $table;
                $data['type'] = 2;
                $data['key'] = $value;
                $data['sort'] = $sort;
                $data['name'] = $fields[$value]?:"";
                $data['ctime'] = time();
                model('form_list_item')->insert($data);
                $sort++;
            }
            $sort = 1;
            if (!in_array($table,self::$roleList)){
                $urlList = ['edit'=>'_edit','enable'=>'enable','disable'=>'disable','del'=>'_del'];
            }else{
                $urlList = ['edit'=>'roleEdit','enable'=>'enable','disable'=>'disable','del'=>'roleDelete'];
            }
            foreach ($list_botton as $key=>$value){
                $data['form_list_id'] = $id;
                $data['table'] = $table;
                $data['type'] = 3;
                $data['key'] = $value;
                $data['sort'] = $sort;
                $data['name'] = Form::$list_botton_array[$value]?:"";
                $data['url'] = $urlList[$value]?:'';
                $data['ctime'] = time();
                model('form_list_item')->insert($data);
                $sort++;
            }

            $sort = 1;
            foreach ($list_title as $key=>$value){
                $data['form_list_id'] = $id;
                $data['table'] = $table;
                $data['type'] = 4;
                $data['key'] = $value;
                $data['sort'] = $sort;
                $data['name'] = $fields[$value]?:"";
                $data['ctime'] = time();
                model('form_list_item')->insert($data);
                $sort++;
            }
            Db::name(CONTROLLER_NAME)->where(['id'=>$id])->update(['status'=>1]);
            admin_return(1,'配置成功');

        }
        $fields = \app\dlc\common\createHelper\Common::getTableInfo($table);
        $data['head_search']['data'] = $fields;
        $data['list_title']['data'] = $fields;
        $this->formAddArray = $data;
        $this->form = 'form_list_ajax';
        parent::_edit_ajax($updata, $add, $noRepeat); // TODO: Change the autogenerated stub
    }

    public function _edit_ajax_callback($id){


//        $name = json_decode($form_list['name']);
    }


    public function _del($tabelName = false, $tableKey = 'id', $child = false, $delChild = false)
    {
//        $child['tableName'] = '';//下级表，如果这个是分类表，使用该分类的表   如:goods 是goods_type的下级
//        $child['key'] = '';//字段 如：goods_type_id
//        $child['msg'] = '该分类下有数据，不能删除';//提示语
        $delChild['tableName'] = 'form_list_item';///下级表，如果这个是分类表，使用该分类的表，删除该分类的数据 如:goods 是goods_type的下级
        $delChild['key'] = 'form_list_id';//字段 如：goods_type_id
        //$child = ['tableName'=>'shop','key'=>'agent_id','msg'=>'该分类下有数据，不能删除'];
        parent::_del($tabelName, $tableKey, $child, $delChild);
    }

    public function addController(){
        $list = FormList::get(input('id'));
        $create = new CreateController();
        $create->create($list['table']);
        admin_return(1,'成功');
    }

    public function addModel(){
        $FormList = FormList::get(input('id'));
        $create = new CreateModel();
        $create->create($FormList['table']);
        admin_return(1,'成功');
    }

//    配置权限列表
    public function ruleList(){
        $this->headBotton('','back','返回',CONTROLLER_URL.'index');
        $this->listBotton('','edit','修改名称',CONTROLLER_URL.'ruleListEidt',2);
        $this->listBotton('','device','设置权限',CONTROLLER_URL.'rule_ajax',2);
        $this->loading();
        $id = input('id');
        $form_list = FormList::get($id);
	    $data = $form_list->data;
        $count = count($data);
        $this->getPage($count, self::$psize, 'App-loader', $this->title, 'App-search');
        $this->assign('data',$data);
        $list = ['id'=>'识别名（唯一）','typeText'=>'类型','name'=>'key','title'=>'中文名','role'=>'权限','sys'=>'操作'];
        $this->assign('list',$list);
        echo $this->fetch('common/index');
    }

    public function rule_ajax(){
	    $post = input('post.');
	    $this->data['id'] = $post['id'];
	    $this->data['role'] = FormList::getList($post['id'],'role');
	    if (count($post)>1){
            FormList::setList($post['id'],implode(',',$post['role']),'role');
	        admin_return(1,'成功');
        }
	    $this->form = 'form_list_role';
	    parent::_edit_ajax();
    }

    public function ruleListEidt(){
        $post = input('post.');
        $formList = new FormList();
        $this->data['id'] = $post['id'];
        $this->data['title'] = FormList::getList($post['id'],'title');
        if (count($post)>1){
            $res = FormList::setList($this->data['id'],$post['title'],'title');
            admin_return(1,'成功');
        }
        $this->form = 'form_list_title';
        parent::_edit_ajax();
    }



}
?>